/**
 * Created by fuhao on 15/9/2.
 */
function UserContract() {
     this.method, this.action, this.operationMethod;
}

UserContract.prototype.search = function (params) {
    var objThis = this;
    var params = params != undefined ? params : $('#inputForm').serializeJson();
    if (!isEmpty(params.createTimeStart) && !isEmpty(params.createTimeEnd)) {
        var start = new Date(params.createTimeStart);
        var end = new Date(params.createTimeEnd);
        if (start > end) {
            validateReqStatus({code: 0, msg: "创建日期：开始时间不能大于结束时间"});
            objThis.bindClickSearch();
            return;
        }
        params.createTimeStart = params.createTimeStart + ' 00:00:00';
        params.createTimeEnd = params.createTimeEnd + ' 23:59:59';
    }
    $('#datagrid').datagrid({
        url: basePath + '/' + this.action + '/' + objThis.method,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        pageSize: 50,
        pageNumber: 1,
        pageList: [20, 50, 100],
        loadMsg: '数据正在加载中...',
        queryParams: params,
        singleSelect: true,
        selectOnCheck: false,
        checkOnSelect: false,
        columns: [[
            {
                field: 'contractCode',
                title: '<span class="txtcenter">租赁合同编号</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'contractValidity',
                title: '<span class="txtcenter">合同有效期</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'houseId',
                title: '<span class="txtcenter">House ID</span>',
                resizable: true,
                hidden: false,
                sortable: true,
                width: '200',
                align: 'center'
            },
            {
                field: 'estateName',
                title: '<span class="txtcenter">小区名称</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'userInfo',
                title: '<span class="txtcenter">客户信息</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'rentPrice',
                title: '<span class="txtcenter">房租</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'remInfo',
                title: '<span class="txtcenter">房管经纪人</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'remOperationInfo',
                title: '<span class="txtcenter">房管运营专员</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'middleAgentInfo',
                title: '<span class="txtcenter">居间经纪人</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'contractCheckStatus',
                title: '<span class="txtcenter">合同审核状态</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'contractStatus',
                title: '<span class="txtcenter">合同状态</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'createTime',
                title: '<span class="txtcenter">创建时间</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center',
                formatter: function (value, row, index) {
                    var dd = new Date(value);
                    return formateDate(dd);
                }
            },
            {
                field: 'userContractId',
                title: '<span class="txtcenter">操作</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center',
                formatter: function (value, row, index) {
                    var html = '<a class="btn btn-sm green" href="' + basePath + '/' + objThis.action + '/' + objThis.operationMethod + '/' + row.userContractId + '">详情</a>';
                    return html;
                }
            }
        ]],
        onLoadSuccess: function (data) {
            validateReqStatus(data);
            objThis.bindClickSearch();
        },
        onLoadError: function (data) {
            validateReqStatus(data);
            objThis.bindClickSearch();
        }
    });
};

UserContract.prototype.bindClickSearch = function () {
    var objThis = this;
    // 搜索
    $('#searchBtn').one('click', function () {
        objThis.search();
    });
};

UserContract.prototype.init = function () {
    var objThis = this;
    objThis.bindClickSearch();
    objThis.bindCascadeClick();
    resetInput();
};

UserContract.prototype.bindCascadeClick = function () {
    // 城市
    $('[name=cityId]').change(function () {
        $('[name=regionId]').empty();
        $('[name=groupId]').empty();
        var cityId = $(this).val();
        if (!cityId) {
            return;
        }
        $.ajax({
            type: 'post',
            url: basePath + '/' + cityId,
            success: function (data) {
                $('[name=regionId]').html(data);
            },
            error: function (data) {
                validateReqStatus(data);
            }
        });
    });
    // 大区
    $('[name=bigAreaId]').change(function () {
        $('[name=groupId]').empty();
        var bigAreaId = $(this).val();
        if (!bigAreaId) {
            return;
        }
        $.ajax({
            type: 'post',
            url: basePath + '/' + bigAreaId,
            success: function (data) {
                $('[name=groupId]').html(data);
            },
            error: function (data) {
                validateReqStatus(data);
            }
        });
    });
};

$(document).ready(function () {
    var userContract = new UserContract();
    userContract.action = "userContract";
    userContract.method = "list";
    userContract.operationMethod = "details";
    userContract.init();
});
